package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.ContentService;
import com.dongdongshop.util.PageBean;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description
 * @ClassName ContentServiceImpl
 * @Author RC
 * @date 2020.12.16 16:21
 */
@Component
@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    private TbContentMapper tbContentMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageBean<TbContent> getContentList(Integer pageNumber, Integer pageSize) {
        PageBean page = new PageBean();
        page.setPageNumber(pageNumber);
        page.setPageSize(pageSize);
        List<TbContent> list = tbContentMapper.getContentList(page.getStartIndex(),pageSize);
        //分页查询
        int i = tbContentMapper.selectCounts();
        page.setTotalCount(i);
        page.setResult(list);
        return page;
    }

    @Override
    public void saveContentCagetory(TbContent tbContent) {
        tbContentMapper.insertSelective(tbContent);
    }

    @Override
    public TbContent getContentListById(Long id) {
        return tbContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateContentCagetory(TbContent tbContent) {
        tbContentMapper.updateByPrimaryKeySelective(tbContent);
    }

    @Override
    public void deleteBatchContent(Long[] ids) {
        tbContentMapper.deleteBatchContent(ids);
    }
    //轮播图展示
    @Override
    public List<TbContent> getContentForBanner() {
        Long ps = Long.valueOf(1);
        /*springbootReis序列化问题*/
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(stringSerializer);
        /*先从redis判断有没有这个key*/
        String tag = "category1:"+ps;//自定义KEY
        ValueOperations redis = redisTemplate.opsForValue();
        String adString = (String)redis.get(tag);//对应string类型
        if(adString == null){ //说明reids没有该数据走数据库
            List<TbContent> tbContents = tbContentMapper.getContentForBanner();
            //在从数据库查询并保存到redis中,利用gson转换工具，将list转换成string
            String s = new Gson().toJson(tbContents);
            redis.set(tag,s);
            return tbContents;
        }else {//redis中有数据，直接在redis中取出
            /*利用gson工具将string转换成List<Map<String, Object>>格式*/
            List<TbContent> list = new Gson().fromJson(adString,new TypeToken<List<TbContent>>(){}.getType());
            return list;
        }
       /* return tbContentMapper.getContentForBanner();*/
    }
    //今日推荐
    @Override
    public List<TbContent> getContenttodayShow() {
        Long ps = Long.valueOf(1);
        /*springbootReis序列化问题*/
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(stringSerializer);
        /*先从redis判断有没有这个key*/
        String tag = "category2:"+ps;//自定义KEY
        ValueOperations redis = redisTemplate.opsForValue();
        String adString = (String)redis.get(tag);//对应string类型
        if(adString == null){ //说明reids没有该数据走数据库
            List<TbContent> tbContents = tbContentMapper.getContenttodayShow();
            //在从数据库查询并保存到redis中,利用gson转换工具，将list转换成string
            String s = new Gson().toJson(tbContents);
            redis.set(tag,s);
            return tbContents;
        }else {//redis中有数据，直接在redis中取出
            /*利用gson工具将string转换成List<Map<String, Object>>格式*/
            List<TbContent> list = new Gson().fromJson(adString,new TypeToken<List<TbContent>>(){}.getType());
            return list;
        }
      /*  return tbContentMapper.getContenttodayShow();*/
    }
    //猜你喜欢

    @Override
    public List<TbContent> getContentLikeShop() {
        Long ps = Long.valueOf(1);
        /*springbootReis序列化问题*/
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(stringSerializer);
        /*先从redis判断有没有这个key*/
        String tag = "category4:"+ps;//自定义KEY
        ValueOperations redis = redisTemplate.opsForValue();
        String adString = (String)redis.get(tag);//对应string类型
        if(adString == null){ //说明reids没有该数据走数据库
            List<TbContent> tbContents = tbContentMapper.getContentLikeShop();
            //在从数据库查询并保存到redis中,利用gson转换工具，将list转换成string
            String s = new Gson().toJson(tbContents);
            redis.set(tag,s);
            return tbContents;
        }else {//redis中有数据，直接在redis中取出
            /*利用gson工具将string转换成List<Map<String, Object>>格式*/
            List<TbContent> list = new Gson().fromJson(adString,new TypeToken<List<TbContent>>(){}.getType());
            return list;
        }
       /* return tbContentMapper.getContentLikeShop();*/
    }
}
